﻿/**
 * CJUtils
 *
 * @author		Allen Chou
 * @version		0.0.2 (last update: Jun 3 2008)
 * @link        http://cjcat.blogspot.com
 * @link		http://cjcat2266.deviantart.com
 */

 /*
 * Copyright 2008 (C) Allen Chou (cjcat2266@gmail.com)
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

package idv.cjcat{
	import flash.errors.IllegalOperationError;

	public class CJUtils {
		
		public static function addUniqueElement(array:Array, element:Object):Object {
			for (var i:int = 0; i < array.length; i++) {
				if (array[i] === element) {
					array.splice(i, 1);
					break;
				}
			}
			
			array.push(element);
			return element;
		}
		
		public static function mergeArrays(array1:Array, array2:Array):Array {
			var temp:Array = new Array();
			for (var i:int = 0; i < array1.length; i++) {
				temp.push(array1[i]);
			}
			for (var j:int = 0; j < array2.length; j++) {
				CJUtils.addUniqueElement(temp, array2[j]);
			}
			return temp;
		}
		
		public static function mergeUniqueElements(host:Array, source:Array):void {
			for (var i:int = 0; i < source.length; i++) {
				for (var j:int = 0; j < host.length; j++) {
					if (host[j] === source[i]) {
						host.splice(j, 1);
						break;
					}
				}
				host.push(source[i]);
			}
		}
		
		public static function removeUniqueElement(array:Array, element:Object):Object {
			for (var i:int = 0; i < array.length; i++) {
				if (array[i] === element) {
					array.splice(i, 1);
					return element;
				}
			}
			
			return null;
		}
		
	}
}